﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CmdServer.Servers;
using System.Threading;

namespace CmdServer
{
    class Matrix
    {
        List<Thread> listThread = new List<Thread>();
        Dictionary<WorkCar, Thread> dicObjThread = new Dictionary<WorkCar, Thread>();
        public Matrix()
        {
            WorkCar wc=new WorkCar(new Count());
            dicObjThread.Add(wc, new Thread(new ThreadStart(wc.DoWork)));
            WorkCar gnt = new WorkCar(new GetNowTime());
            dicObjThread.Add(gnt, new Thread(new ThreadStart(gnt.DoWork)));
        }
        public void Start()
        {
            foreach(var m in dicObjThread.Values)
            {
                m.Start();
            }
        }
        public void Stop()
        {
            foreach (WorkCar wc in dicObjThread.Keys)
            {
                Console.WriteLine("正在结束" + wc.Title);
                while (wc.IsRunning) { Console.WriteLine("等待操作完成"); };
                Console.WriteLine("abort前：" + (dicObjThread[wc].IsAlive ? "alive" : "dead"));
                dicObjThread[wc].Abort();
                Console.WriteLine("abort后：" + (dicObjThread[wc].IsAlive ? "alive" : "dead"));
                dicObjThread[wc].Join();
                Console.WriteLine(wc.Title+(dicObjThread[wc].IsAlive?"alive":"dead"));
                Console.WriteLine("成功结束" + wc.Title);
            }
        }
    }
}
